package com.fh.controller;

import com.fh.common.ResultEnum;
import com.fh.common.ResultMassage;
import com.fh.model.Brand;
import com.fh.service.BrandService;
import com.fh.util.DataTablePageUtil;
import com.fh.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("initqueryBrand")
    public String initqueryBrand(){
        return "brand/brand-Show";
    }

    @RequestMapping("queryBrand")
    @ResponseBody
    public ResultMassage queryBrand(DataTablePageUtil<Brand> page, BrandVo brandVo){
        try{
            DataTablePageUtil<Brand> brandDataTablePageUtil = brandService.queryBrand(page, brandVo);
            return new ResultMassage(ResultEnum.QUERY_SUCCESS_MSG,brandDataTablePageUtil);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.QUERY_ERROR_MSG);
        }

    }

    @RequestMapping("addBrand")
    @ResponseBody
    public ResultMassage addBrand(Brand brand){
        try{
            brandService.addBrand(brand);
            return new ResultMassage(ResultEnum.ADD_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.ADD_ERROR_MSG);
        }
    }


    @RequestMapping("toUpdateBrand")
    @ResponseBody
    public ResultMassage toUpdateBrand(Integer brandId){
        try{
            Brand brand = brandService.toUpdateBrand(brandId);
            return new ResultMassage(ResultEnum.TOUPDATE_SUCCESS_MSG,brand);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.TOUPDATE_ERROR_MSG);
        }
    }


    @RequestMapping("updateBrand")
    @ResponseBody
    public ResultMassage updateBrand(Brand brand){
        try{
            brandService.updateBrand(brand);
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }
    }

    @RequestMapping("deleteBrand")
    @ResponseBody
    public ResultMassage deleteBrand(Integer brandId){
        try{
            brandService.deleteBrand(brandId);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }

    @RequestMapping("bothdeleteBrand")
    @ResponseBody
    public ResultMassage bothdeleteBrand(@RequestParam("arr[]") Integer[] arr){
        try{
            brandService.deleteBothBrand(arr);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }
}
